package com.cwd.arm.common.util;

import com.cwd.arm.common.dto.session.AuthDto;
import com.cwd.arm.common.dto.session.UserDto;
import com.cwd.arm.common.enums.AuthEnum;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Collection;
import java.util.List;

/**
 * Created by yan on 21-12-7.
 */
public class SessionUtil {

    public static UserDto getUser() {
        return (UserDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    public static Long getUserId() {
        UserDto userDto = getUser();
        if (userDto == null) {
            return null;
        }
        return userDto.getId();
    }

    public static List<AuthDto> getAuthList() {
        return getUser().getAuthDtoList();
    }

    public static boolean hasFinAuth() {
        List<AuthDto> authDtoList = getAuthList();
        String fin = AuthEnum.FIN.getCode();
        for (AuthDto authDto : authDtoList) {
            if (authDto.getKey().equals(fin)) {
                return true;
            }
        }
        return false;
    }
}
